package com.example.ymm.util;

import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 时间工具类
 *
 * @author ruoyi
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    private static String[] parsePatterns = {
            "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
            "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
            "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate()
    {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate()
    {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime()
    {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow()
    {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format)
    {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date)
    {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts)
    {
        try
        {
            return new SimpleDateFormat(format).parse(ts);
        }
        catch (ParseException e)
        {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime()
    {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
        try
        {
            return parseDate(str.toString(), parsePatterns);
        }
        catch (ParseException e)
        {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate()
    {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(Date endDate, Date nowDate)
    {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 增加 LocalDateTime ==> Date
     */
    public static Date toDate(LocalDateTime temporalAccessor)
    {
        ZonedDateTime zdt = temporalAccessor.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }

    /**
     * 增加 LocalDate ==> Date
     */
    public static Date toDate(LocalDate temporalAccessor)
    {
        LocalDateTime localDateTime = LocalDateTime.of(temporalAccessor, LocalTime.of(0, 0, 0));
        ZonedDateTime zdt = localDateTime.atZone(ZoneId.systemDefault());
        return Date.from(zdt.toInstant());
    }





    /**
     当天时间区间数组
     */
    public static List<String> getDateListByThisDay(){
        List<String> list=new ArrayList<>();
        Date now = new Date();

        String start="",end="";
        start = DateKit.dateFormat(now, "yyyy-MM-dd 00:00:00");

        end=DateKit.getStringNowTime();

        list.add(start);
        list.add(end);
        return list;
    }

    /**
     * 列出从开始到结束这中间的所有日期
     * @param startDateStr
     * @param endDateStr
     * @param type 0 包含起始和结束日期 1 不包函结束日期 2 不包含起始日期 3 不包含起始和结束日期
     * @return
     */
    public static List<String> getRangeDateCards(String startDateStr, String endDateStr,int type){
        List<String> list=new ArrayList<>();
        List<String> centreList=new ArrayList<>();//中间日期数组
        String[] start_arr = startDateStr.split("-");
        String[] end_arr = endDateStr.split("-");
        LocalDate startDate=LocalDate.of(Integer.parseInt(start_arr[0]),Integer.parseInt(start_arr[1]),Integer.parseInt(start_arr[2]));
        LocalDate endDate=LocalDate.of(Integer.parseInt(end_arr[0]),Integer.parseInt(end_arr[1]),Integer.parseInt(end_arr[2]));
        List<LocalDate> dates = new ArrayList<>();
        while (!startDate.isAfter(endDate)) {
            //dates.add(startDate);
            // 创建一个DateTimeFormatter实例来定义日期格式
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

            // 使用formatter格式化LocalDate
            String formattedDate = startDate.format(formatter);
            if(type==0){
                centreList.add(formattedDate);
            }else if(type==1){
                if(!formattedDate.equals(endDateStr)){
                    centreList.add(formattedDate);
                }
            }
            else if(type==3){
                if(!formattedDate.equals(endDateStr)&&!formattedDate.equals(startDateStr)){
                    centreList.add(formattedDate);
                }
            }

            startDate = startDate.plusDays(1);
        }
//        switch (type){
//            case 0:
//               // list.add(startDateStr);
//                list.addAll(centreList);
//                list.add(endDateStr);
//                break;
//            case 1:
//              //  list.add(startDateStr);
//                list.addAll(centreList);
//                break;
//            case 2:
//                list.addAll(centreList);
//                list.add(endDateStr);
//                break;
//            case 3:
//                list.addAll(centreList);
//                break;
//        }
        return centreList;
    }


    public static List<String> getDateListByThisDayByDate(String date){
        List<String> list=new ArrayList<>();
        String start=date+" 00:00:00",end=date+" 23:59:59";
        list.add(start);
        list.add(end);
        return list;
    }

    /**
     * 获取当月 月初和月末 的时间数组
     * @return
     */
    public static List<String> monthBetweenArr(){
        List<String> list=new ArrayList<>();
        LocalDate today = LocalDate.now();

        int day = today.getDayOfMonth();
        int year = today.getYear();
        int month = today.getMonthValue();

        for (int i = 1; i <day; i++) {
            String str = year + "-" + (month<10?"0"+month:month) + "-" + (i<10?"0"+i:i);
            list.add(str);
        }

        return list;
    }


    /**
     * 获取4天前的日期数组
     * @return
     */
    public static List<String> fourDayBefore(){
        List<String> list=new ArrayList<>();
        Date yesterday = DateKit.getYesterday();
        Date now =DateKit.dateFormat(DateKit.dateFormat(yesterday,"yyyy-MM-dd 00:00:00"));

        for (int i = 4; i >0; i--) {
            Date date1 = DateKit.dateAdd(1, now, -i);
            String str=DateKit.dateFormat(date1,"yyyy-MM-dd");
            list.add(str);

        }
        String yes =DateKit.dateFormat(yesterday,"yyyy-MM-dd");
        list.add(yes);
        return list;
    }


    /**
     * 获取4天后的日期数组
     * @return
     */
    public static List<String> fourDayAfter(){
        List<String> list=new ArrayList<>();
        Date now =DateKit.dateFormat(DateKit.dateFormat(new Date(),"yyyy-MM-dd 00:00:00"));

        for (int i = 0; i <5; i++) {
            Date date1 = DateKit.dateAdd(1, now, i+1);
            String str=DateKit.dateFormat(date1,"yyyy-MM-dd");
            list.add(str);

        }
//        String yes =DateKit.dateFormat(yesterday,"yyyy-MM-dd");
//        list.add(yes);
        return list;
    }

    public static void main(String[] args) {

        System.out.println(monthBetweenArr());

    }

}
